﻿using System;

namespace HomeWork1
{
    public abstract class Shape
    {
        public abstract double getArea();
        public abstract bool isLegal();
    }

    public class Circle : Shape
    {
        private const double PI = 3.14;
        private double radius;
        public Circle() { }
        public Circle(double r)
        {
            radius = r;
        }
        public override double getArea()
        {                     //Circle类求面积
            return PI * radius * radius;
        }
        public override bool isLegal()
        {
            if (radius > 0) return true;
            else return false;
        }
    }

    public class Rectangle : Shape
    {
        private double width;
        private double height;

        public Rectangle() { }
        public Rectangle(double width, double height)
        {
            this.width = width;
            this.height = height;
        }
        public override double getArea()
        {
            return width * height;
        }
        public override bool isLegal()
        {
            if (width > 0 && height > 0) return true;
            else return false;
        }

    }
    public class Square : Shape
    {
        private double length;

        public Square() { }
        public Square(double length)
        {
            this.length = length;
        }
        public override double getArea()
        {
            return length * length;
        }
        public override bool isLegal()
        {
            if (length > 0) return true;
            else return false;

        }
    }


    public class Test
    {
        public Shape createShape()
        {
            Shape sp = null;
            Random rd;
            rd = new Random();
            int j;
            j = rd.Next(3);

            if (j == 0) //圆形
            {
                sp = new Circle(rd.NextDouble());
            }
            else if (j == 1) //长方形
            {
                sp = new Rectangle(rd.NextDouble(), rd.NextDouble());
            }
            else if (j == 2) //正方形
            {
                sp = new Square(rd.NextDouble());
            }
            return sp;
        }

        public static void Main(String[] args)
        {
            Test ot = new Test();
            Shape[] s = new Shape[10];
            for (int i = 0; i < s.Length; i++)
            {
                s[i] = ot.createShape();
            }
            for (int i = 0; i < s.Length; i++)
            {
                Console.Write("第" + (i + 1) + "个形状是：");
                if (s[i] is Circle) Console.WriteLine("圆形");

                else if (s[i] is Rectangle) Console.WriteLine("矩形");

                else if (s[i] is Square) Console.WriteLine("三角形");
            }
            double sum = 0;
            for (int i = 0; i < s.Length; i++)
            {
                sum += s[i].getArea();
            }
            Console.WriteLine("面积之和为： " + sum);
        }
    }

}
